package com.mxpio.erp.mes.service.impl;

import com.mxpio.erp.common.mes.entity.AoiLine;
import com.mxpio.erp.common.mes.entity.AoiMain;
import com.mxpio.erp.common.mes.pojo.Aoi;
import com.mxpio.erp.common.mes.pojo.AoiLs1;
import com.mxpio.erp.common.mes.pojo.AoiLs2;
import com.mxpio.erp.common.mes.service.AoiMainService;
import com.mxpioframework.common.exception.MBootException;
import com.mxpioframework.common.util.BeanReflectionUtils;
import com.mxpioframework.common.vo.Result;
import com.mxpioframework.jpa.policy.CrudContext;
import com.mxpioframework.jpa.policy.impl.SmartCrudPolicyAdapter;
import com.mxpioframework.system.service.impl.BaseServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class AoiMainServiceImpl extends BaseServiceImpl<AoiMain> implements AoiMainService {

    @Override
    @Transactional
    public Result<?> saveFromDl(List<Aoi> jsonObjMap) {
        for (Aoi aoi:jsonObjMap){
            List<AoiLs1> ls1 = aoi.getLs1();
            List<AoiLs2> ls2 = aoi.getLs2();
            if (ls1==null||ls2==null||ls1.size()==0||ls2.size()==0){
                throw new MBootException("接口请求数据不完整");
            }
            Map<String, List<AoiLs2>> collect = ls2.stream().collect(Collectors.groupingBy(AoiLs2::getArrayIndex));
            for (AoiLs1 aoiLs1:ls1){
                AoiMain aoiMain = new AoiMain();
                BeanReflectionUtils.copyProperties(aoiMain,aoi);
                BeanReflectionUtils.copyProperties(aoiMain,aoiLs1);
                String arrayID = aoiLs1.getArrayID();
                List<AoiLine> aoiLines = new ArrayList<>();
                if (collect.containsKey(arrayID)){
                    List<AoiLs2> aoiLs2s = collect.get(arrayID);
                    BigDecimal sucCnt = BigDecimal.ZERO;
                    for (AoiLs2 aoils2:aoiLs2s){
                        if (StringUtils.equalsIgnoreCase("NG",aoils2.getResult())){
                            AoiLine aoiLine = new AoiLine();
                            BeanReflectionUtils.copyProperties(aoiLine,aoils2);
                            aoiLine.setArrayID(arrayID);
                            aoiLines.add(aoiLine);
                        }else{
                            sucCnt = sucCnt.add(BigDecimal.ONE);
                        }
                    }
                    if (sucCnt.compareTo(BigDecimal.ZERO)!=0){
                        AoiLine aoiLine = new AoiLine();
                        aoiLine.setArrayID(arrayID);
                        aoiLine.setBoardBarCode(aoiMain.getBoardBarCode());
                        aoiLine.setResult("Good");
                        aoiLine.setCnt(sucCnt);
                        aoiLines.add(aoiLine);
                    }
                }
                aoiMain.setLines(aoiLines);
                this.save(aoiMain,new SmartCrudPolicyAdapter(){
                    @Override
                    public boolean beforeInsert(CrudContext context) {
                        Object o = context.getEntity();
                        if (o instanceof AoiLine) {
                            AoiMain parent = context.getParent();
                            ((AoiLine) o).setMid(parent.getId());
                        }
                        return true;
                    }
                });
            }
        }
        return Result.OK();
    }
}
